<template>
  <div id="app">
    <div id="chart-container" ref="chartContainer" style="height: 100%; width: 100%; position: absolute; top: 10%; left: 0;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'EchartsDemo',
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
  },
  methods: {
    initChart() {
      const chartContainer = this.$refs.chartContainer;
      this.myChart = echarts.init(chartContainer, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });

      const option = {
        tooltip: {
          confine: true,          // 强制提示框限制在图表区域内
          position: 'right',      // 优先右侧显示（根据实际情况调整）
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['归集总额(万元)', '国税认定额(万元)', '所得税抵扣额(万元)']
        },
        xAxis: {
          type: 'category',
          data: ['2023', '2022', '2021', '2020', '2019', '2018', '2017', '2016', '2015', '2014']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '归集总额(万元)',
            type: 'bar',
            data: [75912.92, 50000, 45000, 67000, 40000, 35000, 30000, 25000, 20000, 15000]
          },
          {
            name: '国税认定额(万元)',
            type: 'bar',
            data: [22000, 40000, 35000, 30000, 25000, 20000, 15000, 10000, 5000, 2000]
          },
          {
            name: '所得税抵扣额(万元)',
            type: 'bar',
            data: [3000, 4000, 3500, 3000, 2500, 2000, 1500, 1000, 500, 200]
          }
        ]
      };

      if (option && typeof option === 'object') {
        this.myChart.setOption(option);
      }
    },
    resizeChart() {
      this.myChart.resize();
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
#chart-container {
  position: relative;
  height: 400px;
  overflow: hidden;
}
</style>
